package cn.com.pc.content.service;

import cn.com.pc.content.domain.content.Content;
import cn.com.pc.content.domain.risk.Risk;
import cn.com.pc.content.domain.risk.RiskRepository;
import cn.com.pc.content.domain.risk.RiskScenes;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class RiskService {

    @Autowired
    private RiskRepository riskRepository;

    public List<Risk> batchCreate(Iterable<Risk> risks) {
        return riskRepository.saveAll(risks);
    }

    public List<Risk> fetch(RiskScenes scenes, int num){
        Risk sample= new Risk();
        Pageable pageable;
        sample.setScenes(scenes);
        pageable = PageRequest.of(1, num);
        return riskRepository.findAll(Example.of(sample), pageable).getContent();
    }

    public int update(Risk risk) {
        riskRepository.saveAndFlush(risk);
        return 0;
    }

    public List<Risk> getByContentAndScenes(Content content, RiskScenes scenes) {
        Risk sample= new Risk();
        Pageable pageable;
        sample.setScenes(scenes);
        sample.setContentId(content.getId());
        pageable = PageRequest.of(1, 1);
        return riskRepository.findAll(Example.of(sample), pageable).getContent();
    }
}
